package org.kunlong.netix.standardserver;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.kunlong.netix.LifeCycle;
import org.kunlong.netix.Server;
import org.kunlong.netix.config.Application;
import org.kunlong.netix.config.servlet.NetixServletContext;
import org.kunlong.netix.container.Container;
import org.kunlong.netix.exception.LifeCycleException;
import org.kunlong.netix.listner.Listner;
import org.kunlong.netix.processor.Processor;

public class StandServer implements Server {
	public static final String SERVER_PATH = System.getProperty("user.dir");
	public static final String WEBAPP_PATH = SERVER_PATH + "/webapp";
	private Listner listner;
	private Processor processor;
	private Container container;
	private List<LifeCycle> component = new ArrayList<LifeCycle>();
	private Map<String, Application> applications = new HashMap<String, Application>();

	public Listner getListner() {
		return listner;
	}

	public Processor getProcessor() {
		return processor;
	}

	public Container getContainer() {
		return container;
	}

	public void setContainer(Container container) {
		this.container = container;
	}
	
	public void init() throws LifeCycleException {
		// TODO Auto-generated method stub
		System.out.println("Server Base Path:" + SERVER_PATH);
		for (LifeCycle life : component) {
			life.init();
		}
		listner.addProcessor(processor);
		loadWebapp();
		initWebapp();
	}

	public void start() throws LifeCycleException {
		// TODO Auto-generated method stub
		for (LifeCycle life : component) {
			life.start();
		}
	}

	public void stop() throws LifeCycleException {
		// TODO Auto-generated method stub
		for (LifeCycle life : component) {
			life.stop();
		}
	}

	public void addContainer(Container c) {
		// TODO Auto-generated method stub
		this.container = c;
		addComponent(c);
	}

	public void addListner(Listner l) {
		// TODO Auto-generated method stub
		this.listner = l;
		addComponent(l);
	}

	public void addProcessor(Processor p) {
		// TODO Auto-generated method stub
		this.processor = p;
		addComponent(p);
	}

	private void addComponent(LifeCycle lifeCycle) {
		this.component.add(lifeCycle);
	}

	private void removeComponent(LifeCycle lifeCycle) {
		this.component.remove(lifeCycle);
	}

	public void removeContainer() {
		removeComponent(container);
		this.container = null;
	};

	public void removeListner() {
		// TODO Auto-generated method stub
		removeComponent(listner);
		this.listner = null;
	}

	public void removePricessor() {
		// TODO Auto-generated method stub
		removeComponent(processor);
		this.processor = null;
	}
	/**
	 * load all the app in the webapp directory
	 * */
	private void loadWebapp(){
		File webAppFile=new File(WEBAPP_PATH);
		if(!webAppFile.exists()||!webAppFile.isDirectory()){
			//the webapp directory is missing
			System.out.println("System Error");
			System.exit(1);
		}
		File[] apps=webAppFile.listFiles();
		for(File app:apps){	
			if(!app.isDirectory()||app.isHidden()){
				continue;
			}
			String defaultWebAppName=app.getName();
			Application application=new NetixServletContext();
			application.setFilename(defaultWebAppName);
			application.setBaseDir(WEBAPP_PATH+"/"+defaultWebAppName);
			applications.put(defaultWebAppName, application);
		}
	}
	
	private void initWebapp()throws LifeCycleException{
		Iterator<Entry<String,Application>> i=applications.entrySet().iterator();
		while(i.hasNext()){
			Entry<String,Application> appEntry=i.next();
			appEntry.getValue().init();
		}
	}
}
